package com.rk.kbk.controller;

import com.rk.kbk.common.JsonResp;
import com.rk.kbk.dao.WebUsersubmitDao;
import com.rk.kbk.dto.PageDto;
import com.rk.kbk.model.WebUsersubmit;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import java.math.BigInteger;
import java.util.List;

/**
 * @WebUsersubmitController
 * @Controller
 * @version : Ver 1.0
 */
@CrossOrigin
@RestController
@Transactional
@RequestMapping(value="/api/webUsersubmit")
public class WebUsersubmitController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private WebUsersubmitDao webUsersubmitDao;
    @PersistenceContext
    private EntityManager entityManager;

    /**
     * @添加/修改
     * @param webUsersubmit
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/addOrUpdateWebUsersubmit",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp addOrUpdateWebUsersubmit(@RequestBody WebUsersubmit webUsersubmit){
        log.debug("添加/修改");
        webUsersubmitDao.save(webUsersubmit);
        return JsonResp.ok();
    }

    /**
     * @批量添加/修改
     * @param webUsersubmits
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/addOrUpdateSomeWebUsersubmit",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp addOrUpdateSomeWebUsersubmit(@RequestBody List<WebUsersubmit> webUsersubmits){
        log.debug("批量添加/修改");
        webUsersubmitDao.save(webUsersubmits);
        return JsonResp.ok();
    }

    /**
     * @获取详情-ByWebUsersubmitId
     * @param webUsersubmitId
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/webUsersubmitDetails",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findWebUsersubmitDetailsByWebUsersubmitId(Long webUsersubmitId) {
        log.debug("获取详情-ByWebUsersubmitId");
        return JsonResp.ok(webUsersubmitDao.findOne(webUsersubmitId));
    }

    /**
     * @分页查询(jpa方式)
     * @param userType 分页条件
     * @param pageNum 第几页
     * @param pageSize 每页的条数
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/jpa/page",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findPageWebUsersubmitJpa(Integer pageNum, Integer pageSize,Integer userType) {
        log.debug("分页查询(jpa方式)");
        //多条件排序Sort
        //Sort sort = new Sort(Sort.Direction.DESC, "createdate").and(new Sort(Sort.Direction.AES, "id"));
        //排序Sort
        Sort sort = new Sort(Sort.Direction.DESC, "gmtDatetime");
        Pageable pageable = new PageRequest(pageNum-1, pageSize, sort);
        Page page = null;
            page = webUsersubmitDao.findAll( 		  //条件分页
                    (root, criteriaQuery, cb) -> {
                        Predicate predicate = cb.conjunction();
                        List<Expression<Boolean>> expressions = predicate.getExpressions();
                        expressions.add(cb.equal(root.get("userType"), userType));
                        return predicate;
                    }, pageable);
//        List<WebUsersubmit> content = page.getContent();
//        for (WebUsersubmit webUsersubmit1 : content) {
//        }
        return JsonResp.ok(new PageDto(page));
    }

    /**
     * @删除-one(永久)
     * @param webUsersubmitId
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/deleteWebUsersubmitOne",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteWebUsersubmitOne(Long webUsersubmitId) {
        log.debug("删除-one(永久)");
        webUsersubmitDao.delete(webUsersubmitId);
        return JsonResp.ok();
    }

    /**
     * @批量删除(永久)
     * @param webUsersubmits
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/deleteSomeWebUsersubmit",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteSomeWebUsersubmit(@RequestBody List<WebUsersubmit> webUsersubmits) {
        log.debug("批量删除(永久)");
        webUsersubmitDao.delete(webUsersubmits);
        return JsonResp.ok();
    }


}
